package com.ict.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ict.common.StateMsg;
import com.ict.model.Summary;
import com.ict.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.util.List;

/**
 * Created by weiaquarius on 16/3/30.
 */
@Controller
@RequestMapping({"/summary"})
public class getSummaryController {
    @Autowired
    SummaryService summaryservice;

    /**
     * http://localhost:8080/alert/summary/getLastSummary.do?insertTime=0&packageType=64&num=2
     * 查询返回指定数量的summary表中最新的记录
     * @param map
     * @param insertTime 数据时间
     * @param packageType 数据类型
     * @param num 返回最新的记录数量
     * @return num条json格式的数据
     */
    @ResponseBody
    @RequestMapping(value = "getLastSummary.do",method = RequestMethod.GET)
    public String getLastSummary(ModelMap map,@RequestParam Double insertTime,@RequestParam Short packageType,@RequestParam Long num) {//num:查询数量
        //insertTime = Long.valueOf();//模拟参数设定
        //packageType = Short.valueof();
        //num = Long.valueOf(50);

        List<Summary> summary = summaryservice.getLastSummary(insertTime,packageType,num);//调用service
        JSONArray jsonArray = new JSONArray();

        for (Summary tmp:summary){
            try {//将jo和jo2两部分放在一个jsonObject中,依次放进jsonArray
                String s = new String(new BASE64Decoder().decodeBuffer(tmp.getJsonDataBase64()));
                JSONObject jo = JSONObject.parseObject(s);
                JSONObject jo2 = (JSONObject) JSONObject.toJSON(tmp);
                jo2.remove("jsonDataBase64");
                JSONObject jsonObject = new JSONObject();
                jsonObject.putAll(jo);
                jsonObject.putAll(jo2);
                jsonArray.add(jsonObject);
            } catch (IOException e) {
                e.printStackTrace();
                map.addAttribute("error","Failure");
                return StateMsg.toJson(-1, map);
            }
        }
        map.addAttribute("list",jsonArray);
        return StateMsg.toJson(1, map);
    }


    @ResponseBody
    @RequestMapping(value = "getSquareSummary.do")
    public String getSquareSummary(ModelMap map,@RequestParam String list) {
        JSONObject jsonObject = JSONObject.parseObject(list);
        JSONArray jsonArray = jsonObject.getJSONArray("areas");
        List<List<Summary>> l = summaryservice.getSquareSummary(jsonArray);
        if(l == null){
            map.addAttribute("error","Failure");
            return StateMsg.toJson(-1, map);
        }
        else {
            map.addAttribute("list",l);
            return StateMsg.toJson(1,map);
        }
    }

    /**
     *http://localhost:8080/alert/summary/getSummaryCount.do?insertTime=0&packageType=48
     * 获得insertTime之后更新数量
     * @param map
     * @param insertTime 查询>这个时间
     * @param packageType 类型
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "getSummaryCount.do",method = RequestMethod.GET)
    public String getSummaryCount(ModelMap map,@RequestParam Double insertTime,@RequestParam Short packageType) {//num:查询数量
        int count= summaryservice.getSummaryCount(insertTime,packageType);
        if(count!=-1){
            map.addAttribute("count",count);
            return StateMsg.toJson(1, map);
        }else {
            map.addAttribute("error","Failure");
            return StateMsg.toJson(-1, map);
        }
    }


}
